package com.smartfast4j.manager.modules.version.controller;

import com.smartfast4j.common.Constants;
import com.smartfast4j.common.annotation.LogRecord;
import com.smartfast4j.common.bean.R;
import com.smartfast4j.common.util.GUIDUtil;
import com.smartfast4j.manager.common.controller.ManagerBaseController;
import com.smartfast4j.manager.modules.version.service.VersionManagerService;
import com.smartfast4j.pojo.version.VersionManager;
import com.smartfast4j.pojo.version.VersionManagerExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* VersionManagerController
*  由代码生成器生成
*/
@RestController
@RequestMapping("/versionManager")
public class VersionManagerController extends ManagerBaseController {

    @Autowired
    private VersionManagerService versionManagerService;

    /***
     * 添加更新版本
     *
     * @param versionManager
     * @return
     */
    @RequestMapping(value = "/add")
    public R add(VersionManager versionManager){
        //先判断一下，该版本号是否已存在
        VersionManager tmp = this.versionManagerService.checkVersionCode(versionManager);
        if (tmp != null) {
            return R.fail("该版本号已经存在");
        } else {
            versionManager.setId(GUIDUtil.genRandomGUID());
            versionManager.setCreateTime(new Date());
            versionManager.setCreateUser(getLoginUserId());
            versionManager.setPublishDate(new Date());
            Integer result = this.versionManagerService.insertSelective(versionManager);
            if(result > 0){
                return R.ok("操作成功");
            }
            return R.fail("操作失败");
        }
    }

    /***
     * 修改数据
     *
     * @param versionManager
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/update")
    public R update(VersionManager versionManager){
        //先判断一下，该版本号是否已存在
        VersionManager tmp = this.versionManagerService.checkVersionCode(versionManager);
        if (tmp != null && !tmp.getId().equals(versionManager.getId())) {
            return R.fail("该版本号已经存在");
        } else {
            versionManager.setUpdateUser(getLoginUserId());
            versionManager.setUpdateTime(new Date());
            Integer result = this.versionManagerService.updateByPrimaryKeySelective(versionManager);
            if(result > 0){
                return R.ok("操作成功");
            }
            return R.fail("操作失败");
        }
    }

    /***
     * 查找所有
     *
     * @param offset  limit
     * @return
     */
    @RequestMapping(value = "/list")
    public R list(Integer offset, Integer limit) {
        //分页
        VersionManagerExample example = new VersionManagerExample();
        example.createCriteria().andDeleteFlagEqualTo(Constants.DELETE_FLAG.NO);
        int count = versionManagerService.countByExample(example);
        example.setLimitStart(offset);
        example.setLimitEnd(limit);
        example.setOrderByClause("create_time desc,update_time desc");
        List<VersionManager> list = versionManagerService.selectByExample(example);
        Map<String,Object> dataMap = new HashMap<>();
        dataMap.put("total",count);
        dataMap.put("rows",list);
        return R.ok(dataMap);
    }

    /**
     * 根据d批量删除
     * @param idList 要删除的id
     * @return
     */
    @LogRecord(operation = "删除",remark = "根据id删除",moduleName = "App版本管理")
    @RequestMapping(value = "/delete")
    public R delete(@RequestBody List<String> idList){
        //删除文件
        versionManagerService.delete(getLoginUserId(), idList);
        return R.ok();
    }
}
